﻿#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <iostream>
#include "SDL3/SDL.h"

bool convertEvent(SDL_Event&, osgGA::EventQueue*);

int runTest2()
{
    if (!SDL_Init(SDL_INIT_VIDEO)) {
        std::cerr << "Unable to init SDL: " << SDL_GetError() << std::endl;
        return 1;
    }

    int W = 800;
    int H = 600;

    SDL_Window *window = SDL_CreateWindow("osgviewerSDL", W, H, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    if (!window) {
        std::cerr << "Unable to create window: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }
    SDL_SetWindowPosition(window, 100, 100);

    SDL_GLContext gl_context = SDL_GL_CreateContext(window);
    if (!gl_context) {
        std::cerr << "Unable to create GL context: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    auto model = osgDB::readNodeFile("glsl_simple.osgt");
    if (!model) {
        std::cout << "No data loaded." << std::endl;
        SDL_Quit();
        return 1;
    }

    osgViewer::Viewer viewer;
    osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gwindow = viewer.setUpViewerAsEmbeddedInWindow(1000, 0, W, H);

    viewer.setSceneData(model);
    viewer.setCameraManipulator(new osgGA::TrackballManipulator);
    viewer.addEventHandler(new osgViewer::StatsHandler);// Key=[S] x1(show) x2 x3 x4 x5(disappear) ......
    viewer.realize();

    bool done = false;
    while (!done) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            convertEvent(event, gwindow->getEventQueue());
            if (event.type == SDL_EVENT_KEY_UP && event.key.key == SDLK_ESCAPE || event.type == SDL_EVENT_QUIT)
                done = true;
        }
        if (done) break;

        viewer.frame();
        SDL_GL_SwapWindow(window);
    }

    SDL_GL_DestroyContext(gl_context);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

bool convertEvent(SDL_Event& event, osgGA::EventQueue* eventQueue) {
    switch (event.type) {
        case SDL_EVENT_MOUSE_MOTION:
            eventQueue->mouseMotion(event.motion.x, event.motion.y);
            return true;
        case SDL_EVENT_MOUSE_BUTTON_DOWN:
            eventQueue->mouseButtonPress(event.button.x, event.button.y, event.button.button);
            return true;
        case SDL_EVENT_MOUSE_BUTTON_UP:
            eventQueue->mouseButtonRelease(event.button.x, event.button.y, event.button.button);
            return true;
        case SDL_EVENT_KEY_UP:
            eventQueue->keyRelease((osgGA::GUIEventAdapter::KeySymbol) event.key.key);
            return true;
        case SDL_EVENT_KEY_DOWN:
            eventQueue->keyPress((osgGA::GUIEventAdapter::KeySymbol) event.key.key);
            return true;
        case SDL_EVENT_WINDOW_RESIZED:
            eventQueue->windowResize(0, 0, event.window.data1, event.window.data2);
            return true;
        default: break;
    }
    return false;
}